package cn.botter.subject.domain.service.impl;

import cn.botter.subject.common.enums.CategoryTypeEnum;
import cn.botter.subject.common.enums.IsDeletedEnum;
import cn.botter.subject.domain.convert.SubjectLabelConverter;
import cn.botter.subject.domain.entity.SubjectLabelBO;
import cn.botter.subject.domain.service.SubjectDomainLabelService;
import cn.botter.subject.infra.basic.entity.SubjectCategory;
import cn.botter.subject.infra.basic.entity.SubjectLabel;
import cn.botter.subject.infra.basic.entity.SubjectMapping;
import cn.botter.subject.infra.basic.service.SubjectCategoryService;
import cn.botter.subject.infra.basic.service.SubjectLabelService;
import cn.botter.subject.infra.basic.service.SubjectMappingService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectDomainLabelServiceImpl implements SubjectDomainLabelService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelController.add.bo:{}", JSON.toJSONString(subjectLabelBO));

        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        return subjectLabelService.insert(subjectLabel);
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelController.update.bo:{}", JSON.toJSONString(subjectLabelBO));

        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        return subjectLabelService.update(subjectLabel);
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectLabelController.delete.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        return subjectLabelService.update(subjectLabel);
    }



    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if (subjectCategory.getCategoryType() == CategoryTypeEnum.BIG_TYPE.getCode()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectCategory.getId());
            List<SubjectLabel> result = subjectLabelService.queryCondition(subjectLabel);
            List<SubjectLabelBO> res = SubjectLabelConverter.INSTANCE.convertLabelToBoList(result);
            return res;
        }

        Integer categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());


        //这里返回的size为空，这里有bug，queryLabelId方法
        List<SubjectMapping> subjectMappings = subjectMappingService.queryLabelId(subjectMapping);

        if (CollectionUtils.isEmpty(subjectMappings)){
            return Collections.emptyList();
        }
        List<Integer> labelIdList = subjectMappings.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach( label ->{
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setCategoryId(categoryId);
            bo.setLabelName(label.getLabelName());
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;
    }


}
